#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/5/23 18:22
# @Author  : khz_df
# @Site    : 
# @File    : rename.py
# @Software: ce9nt


import os
import shutil


def next_pic():
    import win32gui
    import win32api
    import win32con
    classname = "Photo_Lightweight_Viewer"
    titlename = None
    #获取句柄
    hwnd = win32gui.FindWindow(classname, titlename)
    print(hex(hwnd))
    print(win32gui.GetWindowRect(hwnd))

    # control_id = 0xa411
    # wParam = win32con.BN_CLICKED
    # wParam = wParam << 16   # // 左移16位
    # wParam = wParam | 0xa411
    # lParam = 0x00081636  # win32api.GetFindWindowEx(hWndDlgTest, NULL, NULL, TEXT("Say   Hello"));
    # win32api.SendMessage(hwnd, win32con.WM_COMMAND, wParam, lParam)
    # print(win32api.GetLastError())
    # return
    # 点击左键
    hwnd = 0x00081636
    long_position = win32api.MAKELONG(10, 10)
    win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, long_position)
    win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, long_position)


def quick_rename_pic():
    path_src = r'N:\_TMP\captcha\battle_net' '\\'
    path_dst = r'N:\_TMP\captcha\battle_net\result' '\\'

    for i in range(10000):
        print("图片id为：", i)
        pic_name = '%d.jpg' % i
        result = input("Enter your result: ")
        if len(result) != 0:
            shutil.copyfile(path_src + pic_name, path_dst + result + '.jpg')
        next_pic()


def rename_pic():
    pth = r'N:\_TMP\captcha\4399_result'
    os.chdir(pth)

    files = os.listdir(pth)
    print(len(files))
    for f in files:
        print(f)
        if len(f) == len('qh6u.png'):
            pass
        elif len(f) == len('00000000__qh6u.png'):
            try:
                os.rename(f, f[len('00000000__'):])
            except Exception as e:
                os.rename(f, '_bak\\' + f)
        else:
            print('【err】 ', f)
        # return


def rename_pic_battle_net():
    """
    战网验证码过滤小于7个的
    :return:
    """
    min_captcha_len = 7
    pth = r'N:\_TMP\captcha\battle_net_result'
    os.chdir(pth)

    files = os.listdir(pth)
    print(len(files))
    for f in files:
        # print(f)
        if len(f) >= 14:
            print(f)
        continue

        if len(f) - len('.jpg') < min_captcha_len:
            shutil.move(f, '..\\battle_net_result_bak_less7\\' + f)
        else:
            f_png = f[:-4] + '.png'
            shutil.move(f, f_png)


def rename_bn_with_sharp_dir(d):
    os.chdir(d)
    file_list = os.listdir(d)
    for f in file_list:
        print(f)
        if len(f) < 13:
            name = f[:-4]
            print(name)
            name += '#' * (9 - len(name)) + '.png'
            print(name)
            os.rename(f, name)


def rename_bn_with_sharp():
    """
    对齐图片，9位
    :return:
    """
    pic_root_dir = r'N:\_TMP\captcha_prj_bn' '\\'
    train_image_dir = pic_root_dir + "image\\"
    test_image_dir = pic_root_dir + "verify\\"
    rename_bn_with_sharp_dir(train_image_dir)
    rename_bn_with_sharp_dir(test_image_dir)


def rename_bn_delete_sharp():
    pic_root_dir = r'N:\_TMP\captcha\battle_net_result' '\\'
    os.chdir(pic_root_dir)
    file_list = os.listdir(pic_root_dir)
    for f in file_list:
        # print(f)
        f_new = f.replace('#', '')
        print(f, f_new)
        os.rename(f, f_new)


def main():
    rename_bn_delete_sharp()
    # rename_bn_with_sharp()
    # quick_rename_pic()


if __name__ == "__main__":
    print("------------------    Enter __main__    ------------------")

    print(u"[Current work directory is : ]\t" + os.getcwd())
    print(u"[Current process ID is : ]\t" + str(os.getpid()))
    print("\n")
    main()

    print("------------------    Leave __main__    ------------------")
